package com.example.hotel.ease.service.impl;

import com.example.hotel.ease.ex.ServiceCode;
import com.example.hotel.ease.ex.ServiceException;
import com.example.hotel.ease.mapper.UserMapper;
import com.example.hotel.ease.pojo.entity.User;
import com.example.hotel.ease.pojo.param.LoginParam;
import com.example.hotel.ease.pojo.vo.LoginResult;
import com.example.hotel.ease.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public LoginResult login(LoginParam param, Integer userType) {
        User user = userMapper.selectByUsername(param.getUsername());
        if (user == null || user.getType() != userType) {
            throw new ServiceException(ServiceCode.USER_NOT_FOUND, "登录失败，用户名不存在！");
        }

        if (!user.getPassword().equals(param.getPassword())) {
            throw new ServiceException(ServiceCode.PASSWORD_NOT_MATCH, "登录失败，密码错误！");
        }

        LoginResult result = new LoginResult();
        BeanUtils.copyProperties(user, result);
        return result;
    }

}
